//
//  RCCustomTabBar.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-7.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCCustomTabBar.h"
#import "ImageDefs.h"

@implementation RCCustomTabBar{
    Class   _tabbarItemClass;
    id      _delegate;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (id)initWithFrame:(CGRect)frame
       tabBarTitles:(NSArray *)titles
           andClass:(Class)itemClass
         andDelegate:(id)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        _delegate = delegate;
        _tabbarItemClass = itemClass;
        _imgItemLine = [kImgTabItemLine retain];
        self.selectedIndex  = -1;
        
        [self initItemView:titles];
    }
    return self;
}

- (void)initItemView:(NSArray *)titles
{
    int count = [titles count];
    _tabItems = [[NSMutableArray alloc]initWithCapacity:count];
    CGFloat w = self.frame.size.width / count;
    CGFloat h = self.frame.size.height;
    int i = 0;
    for (NSString *title in titles) {
        CGRect frame = CGRectMake(w * i, 0, w, h);
        TabItem *item = [[_tabbarItemClass alloc]initWithFrame:frame];
        
        item.tabItemDelegate = _delegate;
        item.tag = i;
        item.title = title;
        [self addSubview:item];
        [_tabItems addObject:item];
        [item release];
        //for lines
        if (i > 0) {
            UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(w * i - _imgItemLine.size.width / 2.0, (h - _imgItemLine.size.height) / 2.0, _imgItemLine.size.width, _imgItemLine.size.height)];
            [line setImage:_imgItemLine];
            [self addSubview:line];
            [line release];
        }
        i++;
    }
    
}

@end
